package com.huaxindata.im.model.entity;

import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;

import com.huaxindata.im.model.utils.MediaFileUtils;

import java.io.File;

/**
 * @author songxf
 * @date 2018/1/8
 * @Desc $desc$
 * Copyright HUAXIN DATA. All rights reserved.
 */
public class ChatFileMessageBody extends ChatMessageBody implements Parcelable{
    protected ChatFileMessageBody(Parcel in) {
        super(in);
        fileName = in.readString();
        serverTime = in.readString();
        localPath = in.readString();
        remotePath = in.readString();
        fileLength = in.readLong();
        downloadStatus = in.readInt();
    }

    public static final Creator<ChatFileMessageBody> CREATOR = new Creator<ChatFileMessageBody>() {
        @Override
        public ChatFileMessageBody createFromParcel(Parcel in) {
            return new ChatFileMessageBody(in);
        }

        @Override
        public ChatFileMessageBody[] newArray(int size) {
            return new ChatFileMessageBody[size];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        super.writeToParcel(parcel,i);
        parcel.writeString(fileName);
        parcel.writeString(serverTime);
        parcel.writeString(localPath);
        parcel.writeString(remotePath);
        parcel.writeLong(fileLength);
        parcel.writeInt(downloadStatus);
    }

    /**
     * 附件下载状态
     */
    public static class DownloadStatus {
        public static final int DOWNLOADIDEL    = 3;//下载初始状态
        public static final int DOWNLOADING     = 0;//正在下载中
        public static final int DOWNLOADERROR   = 1;//下载失败
        public static final int DOWNLOADSUCCESS = 2;//下载成功
    }


    /**
     * 文件消息体的显示名
     */
    public String fileName;

    /**
     * 服务器时间
     */
    public String serverTime;

    /**
     * 文件消息体的本地文件路径
     */
    public String localPath;

    /**
     * 文件消息体的服务器远程文件路径
     */
    public String remotePath;

    /**
     * 文件消息体的文件长度, 以字节为单位
     */
    public long fileLength;

    /**
     * 附件是否下载完成
     */
    public int downloadStatus;

    public ChatFileMessageBody() {
    }

    public ChatFileMessageBody(int type) {
        super(type);
    }

    public String getFileName() {
        return fileName;
    }

    public String getLocalPath() {
        return localPath;
    }

    public String getRemotePath() {
        return remotePath;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public void setLocalPath(String localPath) {
        this.localPath = localPath;
    }

    public void setRemotePath(String remotePath) {
        this.remotePath = remotePath;
    }

    /**
     * 根据指定的消息字符串来构造messageBody对象
     *
     * @param message
     * @throws NumberFormatException
     */
    public void parseFileMessage(String message) throws NumberFormatException {
        /**
         文件名的长度+分隔符+远程地址名的长度+分隔符+本地路径名的长度+分隔符+文件名+远程地址+本地路径
         17#&61#&55#&S60909-134958.jpghttp://192.168.0.103:8080/uploads/5236/chat/S60909-134958.jpg/storage/emulated/0/Photo/Screenshots/S60909-134958.jpg
         */
        if (TextUtils.isEmpty(message))
            return;
        String[] words = message.split(ChatMessage.PREX);
        if (words.length < 4) {
            return;
        }
        int flen = Integer.parseInt(words[0]);
        int rlen = Integer.parseInt(words[1]);
        String content = words[3];
        this.fileName = content.substring(0, flen);
        this.remotePath = content.substring(flen, flen + rlen);
        this.localPath = content.substring(flen + rlen);

        final File file = new File(localPath);
        if (!file.exists()) {
            this.localPath = "";
        }
    }

    /**
     * 根据messageBody对象拼接消息字符串
     *
     * @return
     */
    public String matchMessage() {
        return fileName.length() + ChatMessage.PREX
                + remotePath.length() + ChatMessage.PREX
                + localPath.length() + ChatMessage.PREX
                + fileName + remotePath + localPath;
    }

    /**
     * 根据本地路径获取media文件的时长
     *
     * @param localPath
     * @return 时长以毫秒为单位
     */
    public synchronized long getMediaLength(String localPath) {
        return MediaFileUtils.getMediaTime(localPath);
    }

    /**
     * 根据本地路径获取文件大小
     *
     * @param localPath
     * @return
     */
    public String getFileSize(String localPath) {

        if (localPath != null && new File(localPath).exists()) {
            long total = new File(localPath).length();
            if (total < 1 << 10) {
                return total + "B";
            } else if (total < 1 << 20) {
                return (total >> 1024 + 1) + "kb";
            } else {
                return (total >> 20) + "M";
            }
        }
        return "";
    }

    public void createFileMessageContent(String localPath){
        File file = new File(localPath);
        if (file.exists()) {
            this.fileName=file.getName();
            this.localPath=file.getAbsolutePath();
            this.remotePath="";
        }
    }

    @Override
    public String toString() {
        return "ChatFileMessageBody{" +
                "fileName='" + fileName + '\'' +
                ", serverTime='" + serverTime + '\'' +
                ", localPath='" + localPath + '\'' +
                ", remotePath='" + remotePath + '\'' +
                ", fileLength=" + fileLength +
                ", downloadStatus=" + downloadStatus +
                '}';
    }
}
